﻿using System.Collections.Generic;
using System.Drawing;
using System.IO;
using EnvDTE;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Actions.Base;
using VSSDKAssist.Assets.Actions.VisualStudio;
using VSSDKAssist.Recipes.Common.Actions;

namespace VSSDKAssist.Recipes.CustomProject.CreateBitmapStrip
{
    public class AddBitmapStripAction : ActionBase
    {
        [Input(Required = true)]
        public List<IconInfo> IconInfoList { get; set; }

        [Input(Required = true)]
        public string BitmapFileName { get; set; }

        [Input(Required = true)]
        public ProjectItem InputProjectItem { get; set; }

        [Output]
        public ProjectItem ProjectItem { get; set; }

        protected override void OnExecute()
        {
            string tempFileName = Path.GetTempFileName();

            using(CreateBitmapStripAction createBitmapStrip = new CreateBitmapStripAction())
            {
                createBitmapStrip.Site = this.Site;
                createBitmapStrip.IconList = GetIconList();
                createBitmapStrip.Execute();

                Bitmap bitmap = createBitmapStrip.Bitmap;
                bitmap.Save(tempFileName);
            }

            using(AddItemFromFileToProjectItemAction addItemFromFileToProjectItem = new AddItemFromFileToProjectItemAction())
            {
                addItemFromFileToProjectItem.Site = this.Site;
                addItemFromFileToProjectItem.SourceFileName = tempFileName;
                addItemFromFileToProjectItem.TargetFileName = this.BitmapFileName;
                addItemFromFileToProjectItem.InputProjectItem = this.InputProjectItem;
                addItemFromFileToProjectItem.Execute();

                this.ProjectItem = addItemFromFileToProjectItem.ProjectItem;
            }

            File.Delete(tempFileName);
        }

        protected override void OnUndo()
        {
            //Do Nothing
        }

        private List<string> GetIconList()
        {
            List<string> iconList = new List<string>();

            foreach(IconInfo iconInfo in this.IconInfoList)
            {
                iconList.Add(iconInfo.FileName);
            }

            return iconList;
        }
    }
}